package com.ruleengine.worker.config;

/**
 * @author lianghanwei on 2024/3/15
 */

import com.ruleengine.destination.DestinationManager;
import com.ruleengine.destination.IDestinationProvider;
import com.ruleengine.destination.helper.RuleEnginePluginManager;
import lombok.Data;
import org.pf4j.PluginManager;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.ruleengine.db.service.RuleService;
import com.ruleengine.db.service.RulechainService;
import com.ruleengine.db.service.RulerelationService;
import com.ruleengine.worker.rulechain.ChainManager;
import com.ruleengine.worker.rulechain.ChainManagerImpl;
import com.ruleengine.worker.runner.chain.CacheChainRunnerManager;
import com.ruleengine.worker.runner.chain.ChainRunnerManager;

@Configuration
@ConfigurationProperties(prefix = "re-worker")
@Data
public class ReWorkerConfig {
    private String destinationFQN;

    // TODO-lhw-2024/03/17: Vertx 实例？

    @Bean
    public ChainManager chainManager(ChainRunnerManager chainRunnerManager,
        RulechainService rulechainService,
        RuleService ruleService,
        RulerelationService rulerelationService) {
        return new ChainManagerImpl(chainRunnerManager, rulechainService, ruleService, rulerelationService);
    }

    @Bean
    public ChainRunnerManager chainRunnerManager(DestinationManager destinationProvider) {
        return new CacheChainRunnerManager(destinationProvider);
    }

    @Bean
    public PluginManager getPluginManager() {
        System.setProperty("pf4j.pluginsDir", "./build/plugins");
        PluginManager pluginManager = new RuleEnginePluginManager();
        pluginManager.loadPlugins();
        pluginManager.startPlugins();
        return pluginManager;
    }

    @Bean
    public DestinationManager getDestinationManager(PluginManager pluginManager) {
        DestinationManager destinationManager = new DestinationManager(pluginManager);
        return destinationManager;
    }

}
